package GUI;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.JOptionPane;
import Kernel.Serveur;
import Kernel.Trajet;
import Kernel.Ville;

public class Etape11 extends javax.swing.JFrame {


	private static final long serialVersionUID = 1L;
	Object[] villes;
	
	
	/** Crée une instance de Etape11() */
	public Etape11(){
		Serveur s = Accueil.serveur;
		villes = s.getReseau().getV().toArray();
		for (int i = 0; i < villes.length; i++) {
			villes[i] = ((Ville) villes[i]).getNom();
		}
	    initComponents();
	    this.setLocationRelativeTo(this.getParent());
	}
	 /**
	   * Initialisation des composants d'AWT.
	  */
	 private void initComponents() {

	        jLabel_etape1 = new javax.swing.JLabel();
	        jLabel_jourD = new javax.swing.JLabel();
	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        jComboBox_vDepart = new javax.swing.JComboBox();
	        jComboBox_vArrivee = new javax.swing.JComboBox();
	        jButton_suivant = new javax.swing.JButton();
	        jButton_retour = new javax.swing.JButton();
	        SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
	        jFormattedTextField_jourD = new javax.swing.JFormattedTextField(format1);
	        jCheckBox_correspondance = new javax.swing.JCheckBox();
	        jCheckBox_distance = new javax.swing.JCheckBox();
	        jSeparator1 = new javax.swing.JSeparator();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	        jLabel_etape1.setText("Etape 1 :");

	        jLabel_jourD.setText("Jour de départ :");

	        jLabel1.setText(" Ville de départ :");

	        jLabel2.setText("Ville d'arrivée :");

	        jComboBox_vDepart.setModel(new javax.swing.DefaultComboBoxModel(villes));

	        jComboBox_vArrivee.setModel(new javax.swing.DefaultComboBoxModel(villes));

	        jButton_suivant.setText("Suivant");
	        jButton_suivant.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                try {
						jButton_suivantActionPerformed(evt);
					} catch (Exception e) {
						e.printStackTrace();
					}
	            }
	        });

	        jButton_retour.setText("Retour");
	        jButton_retour.addActionListener(new java.awt.event.ActionListener() {
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	                jButton_retourActionPerformed(evt);
	            }
	        });

	        jFormattedTextField_jourD.setText("dd/mm/yyyy");
	        jFormattedTextField_jourD.addMouseListener(new java.awt.event.MouseAdapter() {
	            public void mouseClicked(java.awt.event.MouseEvent evt) {
	                jFormattedTextField_jourDMouseClicked(evt);
	            }
	        });

	        jCheckBox_correspondance.setText("Correspondance");

	        jCheckBox_distance.setText("Le plus court");

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(jLabel_etape1)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(244, 244, 244)
	                        .addComponent(jButton_retour)
	                        .addGap(39, 39, 39)
	                        .addComponent(jButton_suivant))
	                    .addGroup(layout.createSequentialGroup()
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(jCheckBox_distance)
	                            .addComponent(jCheckBox_correspondance)
	                            .addComponent(jFormattedTextField_jourD, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
	                            .addComponent(jLabel_jourD))
	                        .addGap(55, 55, 55)
	                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(18, 18, 18)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addGroup(layout.createSequentialGroup()
	                                .addComponent(jLabel1)
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
	                            .addGroup(layout.createSequentialGroup()
	                                .addComponent(jLabel2)
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
	                            .addGroup(layout.createSequentialGroup()
	                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
	                                    .addComponent(jComboBox_vDepart, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                                    .addComponent(jComboBox_vArrivee, javax.swing.GroupLayout.Alignment.LEADING, 0, 186, Short.MAX_VALUE))
	                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))))
	                .addContainerGap(154, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(jLabel_etape1)
	                .addGap(46, 46, 46)
	                .addComponent(jLabel_jourD)
	                .addGap(18, 18, 18)
	                .addComponent(jFormattedTextField_jourD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(110, 110, 110)
	                .addComponent(jCheckBox_correspondance)
	                .addGap(18, 18, 18)
	                .addComponent(jCheckBox_distance)
	                .addGap(56, 56, 56))
	            .addGroup(layout.createSequentialGroup()
	                .addGap(67, 67, 67)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                        .addComponent(jLabel1)
	                        .addGap(17, 17, 17)
	                        .addComponent(jComboBox_vDepart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
	                        .addComponent(jLabel2)
	                        .addGap(18, 18, 18)
	                        .addComponent(jComboBox_vArrivee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(111, 111, 111)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                            .addComponent(jButton_suivant)
	                            .addComponent(jButton_retour)))
	                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addContainerGap())
	        );

	        pack();
	  }
	 
	  private void jButton_retourActionPerformed(java.awt.event.ActionEvent evt) {
		  Utilisateur u = new Utilisateur();
		  this.setVisible(false);
		  u.setVisible(true);	  
	      this.dispose();
	  }
	  
	  private void jFormattedTextField_jourDMouseClicked(java.awt.event.MouseEvent evt) {
	        jFormattedTextField_jourD.setText("");
	  }

	  private void jButton_suivantActionPerformed(java.awt.event.ActionEvent evt) throws Exception {   
		  
		  Serveur s = Accueil.serveur;
		  Ville vDepart = new Ville(jComboBox_vDepart.getSelectedItem().toString());
		  Ville vArrivee = new Ville(jComboBox_vArrivee.getSelectedItem().toString());
		  if(jFormattedTextField_jourD.getText().compareTo("") == 0){
			  JOptionPane.showMessageDialog(this,"Erreur : précisez la date." , "Erreur : Date", JOptionPane.ERROR_MESSAGE);
		  }
		  if(jComboBox_vDepart.getSelectedItem().toString().compareTo(jComboBox_vArrivee.getSelectedItem().toString())==0) {
			  JOptionPane.showMessageDialog(this,"Etes-vous sûre des villes de départ et d'arrivée ?" , "Attention : Ville", JOptionPane.INFORMATION_MESSAGE);
		  }
		  else {
			  String[] buffDate = jFormattedTextField_jourD.getText().split("/");
			  GregorianCalendar g = new GregorianCalendar(Integer.parseInt(buffDate[2]), Integer.parseInt(buffDate[1]),Integer.parseInt(buffDate[0]));
				
			  //Sans correspondance
			  if(!jCheckBox_correspondance.isSelected()){
				  
				  List<List<List<Trajet>>> l = s.getReseau().obtenirItineraires(vDepart, vArrivee, false);
				  if(l.size()==0){
					  PasTransport pt = new PasTransport();
					  this.setVisible(false);
					  pt.setVisible(true);
				  }	
				  else {
					  PropositionTrajet pt = new PropositionTrajet(l,g);
					  this.setVisible(false);
					  pt.setVisible(true);
				  }
			  }
			  //Sinon : avec correspondance
			  else {
				  // Le plus court trajet
				  if(jCheckBox_distance.isSelected()){
					  List<List<Trajet>> l = s.getReseau().itinerairePlusCourt(vDepart, vArrivee, true);
					  if(l.size()==0){
						  PasTransport pt = new PasTransport();
						  this.setVisible(false);
						  pt.setVisible(true);
					  }	
					  else {
						  PropositionTrajet pt = new PropositionTrajet(l);
						  this.setVisible(false);
						  pt.setVisible(true);
					  }
					  
				  }
				  // Tous les trajets avec correspondance
				  else  {
					  List<List<List<Trajet>>> l = s.getReseau().obtenirItineraires(vDepart, vArrivee, true);
					  l.toString();
					  if(l.size()==0){
						  PasTransport pt = new PasTransport();
						  this.setVisible(false);
						  pt.setVisible(true);
					  }	
					  else {
						  PropositionTrajet pt = new PropositionTrajet(l,g);
						  this.setVisible(false);
						  pt.setVisible(true);
					  }
					 
				  }
				  
			  }
			  
		  }
		 
		   
	  }
	
	
	    private javax.swing.JButton jButton_retour;
	    private javax.swing.JButton jButton_suivant;
	    private javax.swing.JCheckBox jCheckBox_correspondance;
	    private javax.swing.JCheckBox jCheckBox_distance;
	    private javax.swing.JComboBox jComboBox_vArrivee;
	    private javax.swing.JComboBox jComboBox_vDepart;
	    private javax.swing.JFormattedTextField jFormattedTextField_jourD;
	    private javax.swing.JLabel jLabel1;
	    private javax.swing.JLabel jLabel2;
	    private javax.swing.JLabel jLabel_etape1;
	    private javax.swing.JLabel jLabel_jourD;
	    private javax.swing.JSeparator jSeparator1;
	
}
